﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;

namespace Tinter.Wpf.Client.Converters
{
    public class LinearGradientBrushConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var brush = (LinearGradientBrush) value;

            int a = GetConvertedColor(brush.GradientStops[1].Color.A, int.Parse((string) parameter));
            int r = GetConvertedColor(brush.GradientStops[1].Color.R, int.Parse((string)parameter));
            int g = GetConvertedColor(brush.GradientStops[1].Color.G, int.Parse((string)parameter));
            int b = GetConvertedColor(brush.GradientStops[1].Color.B, int.Parse((string)parameter));

            var c = new Color() { A = (byte)a, R = (byte)r, G = (byte)g, B = (byte)b };

            return new LinearGradientBrush(c, brush.GradientStops[1].Color, -90);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion

        private static int GetConvertedColor(int baseColor, int parameter)
        {
            int result = baseColor + parameter;

            return result > 255 ? 255 : result < 0 ? 0 : result;
        }
    }
}
